package com.fastadmin.mapper.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.IterUtil;
import cn.hutool.core.util.StrUtil;
import com.fastadmin.pojo.FastParam;
import com.fastadmin.pojo.Resource;
import com.xphsc.easyjdbc.EasyJdbcTemplate;
import com.xphsc.easyjdbc.builder.SQL;
import com.xphsc.easyjdbc.page.PageInfo;
import org.asynchttpclient.util.StringBuilderPool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.stereotype.Repository;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import static org.asynchttpclient.util.StringBuilderPool.DEFAULT;

@Repository
public class ResourceMapper {

    @Autowired
    EasyJdbcTemplate easyJdbcTemplate;

    public void findByRoleId(Long id) {
        StringBuilder sql = DEFAULT.stringBuilder();

    }

    /**
     * 查找所有的资源
     *
     * @return
     */
    public List<Resource> findAll() {
        return this.easyJdbcTemplate.findAll(Resource.class);
    }

    public List<Map<String, Object>> findUrlByUsername(String userName) {
        StringBuilder sql = DEFAULT.stringBuilder();
        sql.append(" SELECT ");
        sql.append(" 	url ");
        sql.append(" FROM ");
        sql.append(" 	t_resource, ");
        sql.append(" 	t_user, ");
        sql.append(" 	t_user_role, ");
        sql.append(" 	t_role_resource, ");
        sql.append(" 	t_role ");
        sql.append(" WHERE ");
        sql.append(" 	1 = 1 ");
        sql.append(" AND t_resource.id = t_role_resource.resource_id ");
        sql.append(" AND t_user.id = t_user_role.user_id ");
        sql.append(" AND t_role.id = t_user_role.role_id ");
        sql.append(" AND t_role.id = t_role_resource.role_id ");
        sql.append(" AND t_user.username =? ");
        return this.easyJdbcTemplate.find(sql.toString(), userName);


    }

    public PageInfo<Resource> findByFastParam(FastParam fastParam) {
        String sql = fastParam.getSql("t_resource");
        return this.easyJdbcTemplate.findByPage(sql, Resource.class, fastParam.getOffset(), fastParam.getLimit());
    }

    public Resource findId(String ids) {
        return this.easyJdbcTemplate.getByPrimaryKey(Resource.class, ids);

    }

    public int deleteByIds(String ids) {
        String[] split = StrUtil.split(ids, ",");
        return this.easyJdbcTemplate.deleteByIds(Resource.class, CollUtil.toList(split));
    }

    public int updateByPrimaryKey(Resource resource) {
        return this.easyJdbcTemplate.update(resource);
    }

    public Object save(Resource resource) {
        resource.setCreate_time(new Timestamp(System.currentTimeMillis()));
        resource.setCreate_time(new Timestamp(System.currentTimeMillis()));
        return this.easyJdbcTemplate.insertKey(resource);

    }

    /**
     * 查询所有的父菜单
     *
     * @return
     */
    public List<Resource> findTypeResource(Iterable type) {
        SQL sql = SQL.BUILD()
                .SELECT("*")
                .FROM("t_resource")
                .WHERE("onemenu in (" + CollUtil.join(type, ",") + ")")
                .ORDER_BY("create_time DESC");
        return this.easyJdbcTemplate.find(sql, Resource.class);
    }

    /**
     * 查询出来有权限的菜单
     *
     * @param type
     * @param authorities
     * @return
     */
    public List<Resource> findTypeResourceAndRole(Iterable<Integer> type, Collection<? extends GrantedAuthority> authorities) {
        StringBuilder sql = DEFAULT.stringBuilder();
        sql.append(" SELECT ");
        sql.append(" 	t_resource.* ");
        sql.append(" FROM ");
        sql.append(" 	t_resource, ");
        sql.append(" 	t_role, ");
        sql.append(" 	t_role_resource ");
        sql.append(" WHERE ");
        sql.append(" 	1 = 1 ");
        sql.append(" AND t_resource.id = t_role_resource.resource_id ");
        sql.append(" AND t_role.id = t_role_resource.role_id ");

        sql.append(" AND  FIND_IN_SET (t_role.role_key,?)");
        sql.append(" AND  FIND_IN_SET (t_resource.onemenu,?)");

        return this.easyJdbcTemplate.find(sql.toString(),
                Resource.class,
                CollUtil.join(authorities, ","),
                CollUtil.join(type, ",")

        );

    }
}

